<?php
#基础函数
include_once('_path.php');

if (BENCHMARK) {
	require_once(_CONST_LOCAL_INCLUDE_PATH.'/Benchmark/Timer.php');
	$timer = new Benchmark_Timer();
	$timer->start();
}

#db
include_once(_CONST_LOCAL_INCLUDE_PATH."database.class.php");
$database = new Database($db_vars);

if ($db_slave_vars['hostname'] != '') {
	$slave = new Database($db_slave_vars);
} else {
	$slave =& $database;
}

#通用函数
include_once(_CONST_LOCAL_INCLUDE_PATH."common.php");

if (BENCHMARK) $timer->setMarker('init');

#登录权限
check_loggedin();

#用户信息
include_once(_CONST_LOCAL_INCLUDE_PATH."session.include.php");

if (BENCHMARK) $timer->setMarker('auth');

if (isset($_POST['act']) && $_POST['act'] == 'setting') {
    $token = $_POST['token'];
	if(empty($token) || $token != $_SESSION['token']){
		exit("Access Exit!");
	}
	
	$sex = $_POST['sex'];
	$intro = RemoveXSS(trim($_POST['intro']));
	$year = $_POST['year'];
	$month = $_POST['month'];
	$day = $_POST['day'];
	$area1 = $_POST['area1'];
	$area2 = $_POST['area2'];
	$area3 = $_POST['area3'];
	
	$birthday = $year.','.$month.','.$day;
	if($area1 == '请选择')
	{
		$location = '';
	}else{
		$location = $area1.','.$area2.','.$area3;
	}
	
	$database->execute("UPDATE user_info SET sex=".$sex.",birthday=".string_sql($birthday).",intro=".string_sql($intro).iif(strlen($location)>0, ",location=".string_sql($location))." WHERE user_id=".$_SESSION['userid']);
}

$token = md5(uniqid().time());
$_SESSION['token'] = $token;

#载入model
load_model(array("user"));
//获得用户资料
$rs = get_userinfo();
#cache lite
include_once(_CONST_LOCAL_INCLUDE_PATH.'Cache/Lite.php');
include_once(_CONST_LOCAL_INCLUDE_PATH.'Cache/Lite/Output.php');

$cache = new Cache_Lite_Output();

#用于meta的keywords标签
$keywords = "";
#用于meta的Description标签
$description = "";
#用于mete的http-equiv标签
$referrer = "";
#用于meta的title标签
$title = "设置 - Votings";
#用于获取针对本页的css样式
$css = array();
#用于获取针对本页的js
$js = array("provincesdata", "jquery.provincesCity");
#用于获取针对head的其他标签
$links = array();
#用于控制那个主菜单为
$o = "";

$ads = array();

$post_code =<<<HTML
HTML;

if (BENCHMARK) $timer->setMarker('proc');

//if ($user['loggedin'] < 1)
//	cache_header(900);

no_cache_header();

#header
include_once(_CONST_LOCAL_LAYOUT_PATH."_header.php");
?>
<script>
$(function(){
    /*
     * 生成级联菜单
     */
    var i=1945;
    var date = new Date();
    year = date.getFullYear();//获取当前年份    
    var dropList;
    for(i;i<2012;i++){        
        if(i == year){
            dropList = dropList + "<option value='"+i+"' selected>"+i+"</option>";
        }else{
            dropList = dropList + "<option value='"+i+"'>"+i+"</option>";
        }        
    }
    $('select[name=year]').html(dropList);//生成年份下拉菜单
    var monthly;
    for(month=1;month<13;month++){
        monthly = monthly + "<option value='"+month+"'>"+month+"</option>";
    }
    $('select[name=month]').html(monthly);//生成月份下拉菜单
    var dayly;
    for(day=1;day<=31;day++){
        dayly = dayly + "<option value='"+day+"'>"+day+"</option>";
    }
    $('select[name=day]').html(dayly);//生成月份下拉菜单
    /*
     * 处理每个月有多少天---联动
     */
    $('select[name=month]').change(function(){
        var currentDay;
        var Flag = $('select[name=year]').val();
        var currentMonth = $('select[name=month]').val();
        switch(currentMonth){
            case "1" : 
            case "3" :
            case "5" :
            case "7" :
            case "8" :
            case "10" :
            case "12" :total = 31;break;
            case "4" :
            case "6" :
            case "9" :
            case "11" :total = 30;break;
            case "2" :
                if((Flag%4 == 0 && Flag%100 != 0) || Flag%400 == 0){
                    total = 29;
                }else{
                    total = 28;
                }
            default:break;                
        }        
        for(day=1;day <= total;day++){
            currentDay = currentDay + "<option value='"+day+"'>"+day+"</option>"; 
        }
        $('select[name=day]').html(currentDay);//生成日期下拉菜单
        }),
		$("#area").ProvinceCity();
})
</script>
<div id="main">
<form name="settingForm" id="settingForm" action="settings.php" method="post">
<input name="token" id="token" type="hidden" value="<?php echo $token;?>">
<input name="act" id="act" type="hidden" value="setting">
	<div class="box">
		<div class="box-header"><h1>设置</h1></div>
		<div class="input-wrap clearfix">
			<label for="">性别</label>
			<input type="radio" name="sex" value="1" <?php echo iif($rs['sex']==1, "checked='checked'")?>>男
			<input type="radio" name="sex" value="2" <?php echo iif($rs['sex']==2, "checked='checked'")?>>女
			<input type="radio" name="sex" value="0" <?php echo iif($rs['sex']==0, "checked='checked'")?>>保密
		</div>
		<div class="input-wrap clearfix">
			<label for="">生日</label>
			<select name="year" id="year">
			</select>年
			<select name="month" id="month">     
			</select>月
			<select name="day" id="day"> 
			</select>日
		</div>
		<?php
		if(!empty($rs['birthday']))
		{
			list($year, $month, $day) = explode(',',$rs['birthday']);
		?>
		<script type="text/javascript">
			jQuery(function($) {
			   $('#year').append("<option selected='selected'><?=$year?></option>");
			   $('#month').append("<option selected='selected'><?=$month?></option>");
			   $('#day').append("<option selected='selected'><?=$day?></option>");
			});
		</script>		
		<?php
		}
		?>
		<div class="input-wrap clearfix" id="area">
		<label for="">城市</label>
		</div>
	  <?php
		if(!empty($rs['location'])){
			list($area1, $area2, $area3) = explode(',', $rs['location']);
		?>
		<script type="text/javascript">
			jQuery(function($) {
			   $('#area1').append("<option selected='selected'><?=$area1?></option>");
			   $('#area2').append("<option selected='selected'><?=$area2?></option>");
			   $('#area3').append("<option selected='selected'><?=$area3?></option>");
			});
		</script>
	   <?php
		}
	   ?>
		<div class="input-wrap clearfix">
			<label for="">介绍</label>
			<textarea class="add-user-about" name="intro" <?php if(empty($rs['intro'])){ ?>onfocus="this.value='';" onblur="if(this.value==''){this.value='Some words about why would you like to buy this item or why you feel uncertain about it.'}"<?php } ?>><?php echo iif($rs['intro']!='', $rs['intro'],"Some words about why would you like to buy this item or why you feel uncertain about it.")?></textarea>
		</div>
		<input type="submit" name="next-step" class="add-save" value="" />
	</div>
</form>	
</div>
<!--main end-->
<?php
//footer
include_once (_CONST_LOCAL_LAYOUT_PATH."_footer.php");

if (BENCHMARK) $timer->setMarker('page');

if (BENCHMARK) {
	echo "<!-- init: ".$timer->timeElapsed('Start', 'init')." -->\n";
	echo "<!-- auth: ".$timer->timeElapsed('init', 'auth')." -->\n";
	echo "<!-- proc: ".$timer->timeElapsed('auth', 'proc')." -->\n";
	echo "<!-- page: ".$timer->timeElapsed('proc', 'page')." -->\n";
	echo "<!-- total: ".$timer->timeElapsed('Start', 'Stop')." -->\n";
}

?>
